﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;

namespace Sistema_de_Taller.SDT_UI.WorkOrder
{
    public partial class workOrder_details : System.Web.UI.UserControl
    {

        public static UInt32 WorkOrderID;
        public static DataTable tmpWorkDone, tmpReplacement, tmpWorker;
        public static bool ReplacementOkey = false;

        private void Page_Init(object sender, System.EventArgs e)
        {

            WorkOrderID = Convert.ToUInt32(Request.QueryString["Id"]);
            //DataTable Vehicle;
            cargarForms(WorkOrderID);
            
            
            if (!IsPostBack)
            {
                initializeTables();
                grdReplacement.DataSource = loadReplacement(WorkOrderID);
                grdWorkDone.DataSource = loadWorkDone(WorkOrderID);
                grdWorker.DataSource = loadWorker(WorkOrderID);

            }

            grdReplacement.Rebind();
            grdWorkDone.Rebind();
            grdWorker.Rebind();
        }

        
        protected static void initializeTables()
        {
            tmpReplacement = new DataTable();
            tmpWorkDone = new DataTable();
            tmpWorker = new DataTable();
            initializeWorkDone();
            initializeReplacement();
            initializeWorker();
            
        }

        protected static DataTable loadReplacement(UInt32 WorkOrderID)
        {
            DataTable Replacement;
            SDT_DAL.Utilities.ReplacementUsed_DAL ReplacementData = new SDT_DAL.Utilities.ReplacementUsed_DAL();
            Replacement = ReplacementData.ReplacementUsedLoadID(WorkOrderID).Tables[0];

            foreach (DataRow Fila in Replacement.Rows)
            {
                DataRow Row = tmpReplacement.NewRow();
                //ver ese asunto del ID
                Row["ID / Nombre"] = Fila[0];
                Row["Cantidad Utilizada"] = Fila[1].ToString();
                //Row["Estado"] = 0;
                //SDT_DAL.Utilities.Vehicle_DAL modifico = new SDT_DAL.Utilities.Vehicle_DAL();
                tmpReplacement.Rows.Add(Row);
            }
            return tmpReplacement;
        }

        protected static DataTable loadWorkDone(UInt32 WorkOrderID)
        {
            DataTable WorkDone;
            SDT_DAL.Utilities.WorkDone_DAL WorkDoneData = new SDT_DAL.Utilities.WorkDone_DAL();
            WorkDone = WorkDoneData.WorkDonerLoadID(WorkOrderID).Tables[0];

            foreach (DataRow Fila in WorkDone.Rows)
            {
                DataRow Row = tmpWorkDone.NewRow();

                Row["Trabajo Realizado"] = Fila[1].ToString().Trim();
                Row["Horas de Trabajo"] = Fila[2].ToString().Trim();
                //Row["Estado"] = 0;
                //SDT_DAL.Utilities.Vehicle_DAL modifico = new SDT_DAL.Utilities.Vehicle_DAL();
                tmpWorkDone.Rows.Add(Row);
            }
            return tmpWorkDone;
        }

        protected static DataTable loadWorker(UInt32 WorkOrderID)
        {
            DataTable Worker;
            SDT_DAL.Utilities.AssignedEmployee_DAL EmployeeData = new SDT_DAL.Utilities.AssignedEmployee_DAL();
            Worker = EmployeeData.AssignedEmployeerLoadID(WorkOrderID).Tables[0];

            foreach (DataRow Fila in Worker.Rows)
            {
                DataRow Row = tmpWorker.NewRow();

                Row["ID / Nombre"] = Fila[0];
                Row["Horas Trabajadas"] = Fila[1].ToString();
                //Row["Estado"] = 0;
                //SDT_DAL.Utilities.Vehicle_DAL modifico = new SDT_DAL.Utilities.Vehicle_DAL();
                tmpWorker.Rows.Add(Row);
            }
            return tmpWorker;
        }


        private void cargarForms(UInt32 WorkOrderID)
        {
            DataSet WorkOrder;

            SDT_DAL.Utilities.WorkOrder_DAL WorkOrderData = new SDT_DAL.Utilities.WorkOrder_DAL();
            WorkOrder = WorkOrderData.WorkOrderLoadID(WorkOrderID);
          
            //Cargo datos WorkOrder

            cmbType.SelectedValue= WorkOrder.Tables[0].Rows[0].ItemArray[0].ToString().Trim();
            cmbClient.Text = WorkOrder.Tables[0].Rows[0].ItemArray[1].ToString().Trim();
            dpEntry.SelectedDate = Convert.ToDateTime(WorkOrder.Tables[0].Rows[0].ItemArray[2]);
            dpClose.SelectedDate = Convert.ToDateTime(WorkOrder.Tables[0].Rows[0].ItemArray[3]); ;
            cmbState.SelectedValue= WorkOrder.Tables[0].Rows[0].ItemArray[4].ToString().Trim(); ;
        }

        protected static void initializeWorkDone()
        {
            DataColumn WorkDone_Realized;
            DataColumn WorkDone_Hours;


            WorkDone_Realized = new DataColumn();
            WorkDone_Realized.DataType = Type.GetType("System.String");
            WorkDone_Realized.ColumnName = "Trabajo Realizado";
            tmpWorkDone.Columns.Add(WorkDone_Realized);

            WorkDone_Hours = new DataColumn();
            WorkDone_Hours.DataType = Type.GetType("System.UInt32");
            WorkDone_Hours.ColumnName = "Horas de trabajo";
            tmpWorkDone.Columns.Add(WorkDone_Hours);

        }

        protected static void initializeReplacement()
        {
            DataColumn Replacement_ID_Nombre;
            DataColumn Replacement_Output;

            Replacement_ID_Nombre = new DataColumn();
            Replacement_ID_Nombre.DataType = Type.GetType("System.String");
            Replacement_ID_Nombre.ColumnName = "ID / Nombre";
            tmpReplacement.Columns.Add(Replacement_ID_Nombre);


            Replacement_Output = new DataColumn();
            Replacement_Output.DataType = Type.GetType("System.UInt32");
            Replacement_Output.ColumnName = "Cantidad Utilizada";
            tmpReplacement.Columns.Add(Replacement_Output);

        }

        protected static void initializeWorker()
        {
            DataColumn Worker_Name;
            DataColumn Worker_Hours;


            Worker_Name = new DataColumn();
            Worker_Name.DataType = Type.GetType("System.String");
            Worker_Name.ColumnName = "ID / Nombre";
            tmpWorker.Columns.Add(Worker_Name);

            Worker_Hours = new DataColumn();
            Worker_Hours.DataType = Type.GetType("System.UInt32");
            Worker_Hours.ColumnName = "Horas Trabajadas";
            tmpWorker.Columns.Add(Worker_Hours);

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAddWorkDone_Click(object sender, EventArgs e)
        {
            pnlWorkDone.Visible = true;
            DataRow Fila = tmpWorkDone.NewRow();
            Fila["Trabajo Realizado"] = txtWorkDone.Text;
            Fila["Horas de Trabajo"] = txtHours.Text;
            tmpWorkDone.Rows.Add(Fila);
            grdWorkDone.DataSource = tmpWorkDone;
            grdWorkDone.Rebind();
        }

        protected void btnAddReplacement_Click(object sender, EventArgs e)
        {
           /* DataRow Fila = tmpReplacement.NewRow();
            Fila["ID / Nombre"] = cmbName.SelectedItem.Value;
            Fila["Cantidad Utilizada"] = Convert.ToUInt32(txtQuantity.Text);
            tmpReplacement.Rows.Add(Fila);
            grdReplacement.DataSource = tmpReplacement;
            grdReplacement.Rebind();*/
        }

        protected void btnAddWorker_Click(object sender, EventArgs e)
        {
            DataRow Fila = tmpWorker.NewRow();
            Fila["ID / Nombre"] = cmbEmployee.SelectedItem.Value;
            Fila["Horas Trabajadas"] = Convert.ToUInt32(txtHoursWorked.Text);
            tmpWorker.Rows.Add(Fila);
            grdWorker.DataSource = tmpWorker;
            grdWorker.Rebind();
        }

        protected void btnCreateWorkOrder_Click(object sender, EventArgs e)
        {
            Common_Files.Definitions.WorkOrder WorkOrder = new Common_Files.Definitions.WorkOrder();
            MessageOk.Visible = false;
            WorkOrder.WorkOrder_Type = cmbType.SelectedValue.ToString();
            WorkOrder.WorkOrder_Account = Convert.ToUInt32(cmbClient.SelectedValue);
            WorkOrder.WorkOrder_Entry = dpEntry.SelectedDate.Value;
            WorkOrder.WorkOrder_Close = dpClose.SelectedDate.Value;
            WorkOrder.WorkOrder_State = cmbState.SelectedValue.ToString();
            WorkOrder.WorkOrder_ID = WorkOrderID;

            SDT_BS.WorkOrder_BS manageWorkOrder = new SDT_BS.WorkOrder_BS();
            manageWorkOrder.insert(WorkOrder, tmpWorkDone.Rows, tmpReplacement.Rows, tmpWorker.Rows, 0);

            lblMessages.Text = "Orden de Trabajo modificada satisfactoriamente";
            MessageOk.Visible = true;
        }

        protected void validateStock(Object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
        {
            MessageOk.Visible = false;
            SDT_DAL.Utilities.Replacement_DAL Repuesto = new SDT_DAL.Utilities.Replacement_DAL();
            args.IsValid = Repuesto.ReplacementStockExist(Convert.ToUInt32(cmbName.SelectedItem.Value.Split(' ')[0]), Convert.ToUInt32(txtQuantity.Text));

            if (args.IsValid && ReplacementOkey)
            {
                DataRow Fila = tmpReplacement.NewRow();
                Fila["ID / Nombre"] = cmbName.SelectedItem.Value;
                Fila["Cantidad Utilizada"] = Convert.ToUInt32(txtQuantity.Text);
                tmpReplacement.Rows.Add(Fila);
                grdReplacement.DataSource = tmpReplacement;
                grdReplacement.Rebind();

            }

        }

        protected void validateReplacement(Object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
        {
            MessageOk.Visible = false;
            bool existe = false;

            foreach (DataRow Fila in tmpReplacement.Rows)
            {
                Int32 ID = Convert.ToInt32(Fila[0].ToString().Trim().Split(' ')[0]);
                if (ID == Convert.ToInt32(cmbName.SelectedItem.Value.Split(' ')[0])) existe = true;
            }

            args.IsValid = !existe;

            if (args.IsValid) ReplacementOkey = true;
            else ReplacementOkey = false;

        }
    }
}